#include <graphics.h>      
#include <conio.h>
#include<ctime>
#include<cstdlib>

int x = 20, y = 20;
bool position[1505][605];
const int sleepTime = 0;

void left();
void right();
void up();
void down();
int getRandData(int a, int b);
bool Judge(int x, int y);
bool Judge2(int a, int b);
void displayGrass();
void displayBox();

int main()
{
	initgraph(1500, 600);
	displayGrass();
	displayBox();

	setcolor(YELLOW);
	setfillcolor(YELLOW);
	fillcircle(x, y, 10);

	char c = 0;
	while (1) {
		if (_kbhit()) {
			c = _getch();
			if (c == 'a') {//left
				for (int i = 1;i<=5; x -= 1,i++) {
					if (x == 10 || position[x - 10][y])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);

					/*if (_kbhit())
					{
						c = _getch();
						break;
					}*/
				}
			}
			if (c == 'd') {//right
				for (int i=1;i<=5;i++, x += 1) {
					if (x == 1490 || position[x + 10][y])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);


					/*if (_kbhit())
					{
						c = _getch();
						break;
					}*/
				}
			}
			if (c == 'w') {//up
				for (int i=1;i<=5;i++, y -= 1) {
					if (y == 10 || position[x][y - 10])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);

					/*if (_kbhit())
					{
						c = _getch();
						break;
					}*/
				}
			}
			if (c == 's') {//down
				for (int i=1;i<=5;i++, y += 1) {
					if (y == 590 || position[x][y + 10])
						break;
					setcolor(YELLOW);
					setfillcolor(YELLOW);
					fillcircle(x, y, 10);
					Sleep(sleepTime);
					setcolor(BLACK);
					setfillcolor(BLACK);
					fillcircle(x, y, 10);

					/*if (_kbhit())
					{
						c = _getch();
						break;
					}*/
				}
			}
			if (c == 27) {
				break;
			}
			setcolor(YELLOW);
			setfillcolor(YELLOW);
			fillcircle(x, y, 10);
		}
			
		/*switch (c) {
		case 'a': left(); break;
		case 'd': right(); break;
		case 'w': up(); break;
		case 's': down(); break;
		case 27: exit(0);
		}*/
	}
	
	return 0;
}

void displayGrass() {                      //grassSquare: 300*200   color:green
	srand((unsigned)time(NULL));
	int a, b;
	for (int i = 1; i <= 3; i++) {
		a = getRandData(0, 1200);
		b = getRandData(0, 400);
		while (Judge(a, b)) {
			a = getRandData(0, 1200);
			b = getRandData(0, 400);
		}
		setcolor(GREEN);
		setfillcolor(GREEN);
		fillrectangle(a, b, a + 300, b + 200);
		for (int j = a; j <= a + 300; j++)
			for (int k = b; k <= b + 200; k++) {
				position[j][k] = 1;
			}
	}
}

int getRandData(int a, int b)
{
	return (rand() % (b - a + 1)) + a;
}

bool Judge(int a, int b) {
	if (position[a][b])
		return 1;
	if (position[a + 300][b])
		return 1;
	if (position[a][b + 200])
		return 1;
	if (position[a + 300][b + 200])
		return 1;
	return 0;
}

bool Judge2(int a, int b) {
	if (position[a][b])
		return 1;
	if (position[a + 100][b])
		return 1;
	if (position[a][b + 100])
		return 1;
	if (position[a + 100][b + 100])
		return 1;
	return 0;
}

void left() {
	char c = 0;
	for (;; x -= 2) {
		if (x == 10 || position[x - 10][y])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	right();
}

void right() {
	char c = 0;
	for (;; x += 2) {
		if (x == 1490 || position[x + 10][y])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);


		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	left();
}

void up() {
	char c = 0;
	for (;; y -= 2) {
		if (y == 10 || position[x][y - 10])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	down();
}

void down() {
	char c = 0;
	for (;; y += 2) {
		if (y == 590 || position[x][y + 10])
			break;
		setcolor(YELLOW);
		setfillcolor(YELLOW);
		fillcircle(x, y, 10);
		Sleep(15);
		setcolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(x, y, 10);

		if (_kbhit())
		{
			c = _getch();
			break;
		}
	}
	switch (c) {
	case 0:break;
	case 'a': left(); break;
	case 'd': right(); break;
	case 'w': up(); break;
	case 's': down(); break;
	case 27: exit(0);
	}
	up();
}

void displayBox() {     //盒子大小100*100，初始15个, color:brown
	srand((unsigned)time(NULL));
	int a, b;
	for (int i = 1; i <= 15; i++) {
		a = getRandData(0, 1400);
		b = getRandData(0, 500);
		while (Judge2(a, b)) {
			a = getRandData(0, 1400);
			b = getRandData(0, 500);
		}
		setcolor(BROWN);
		setfillcolor(BROWN);
		fillrectangle(a, b, a + 100, b + 100);
		for (int j = a; j <= a + 100; j++)
			for (int k = b; k <= b + 100; k++) {
				position[j][k] = 1;
			}
	}
}